<?php

namespace app\beautiful\model\order;

use app\beautiful\model\partner\PartnerCommissionModel;
use app\common\model\CommonModel;

use app\beautiful\model\member\MemberModel;

use app\beautiful\model\order\GoodsOrderItemModel;

class CashierOrderModel extends CommonModel{

    protected  $name = 'beautiful_cashier_order';

    protected  $pk   = 'order_id';

    protected $type=[

        'pay_info' => 'array',

    ];



    public function member()

    {

        return $this->hasOne(MemberModel::class,'member_id','member_id');

    }

    

    public function getPayTimeFormatAttr($value,$data){

        return $data['pay_time'] ? date('Y-m-d H:i:s',$data['pay_time']) : '-';

    }

    

    public function getOrderTimeAttr($value,$data){

        return date('Y-m-d H:i',$data['add_time']);

    }

    

    public function getDjsTimeAttr($value,$data){

        $t = time();

        $djs = $data['add_time'] + 900 - $t; //15分钟有效

        return  $djs > 0 ?  $djs : 0;

    }

    

    public function getYouhuiAttr($value,$data){

        $youhui = round(($data['integral_balance'] * 100 + $data['coupon_money'] * 100 + $data['youhui_balance'] * 100)/100,2);

        return $youhui;

    }

    

    public function skus(){

        return  $this->hasMany(CashierOrderItemModel::class,'order_id','order_id');

    }

    

    public function getPayTypeMeanAttr($value,$data)

    {

        $type_means=[

            'weixin'=>'微信支付',

            'money'=>'余额支付',

            'cash' => '现金支付',

        ];

        return $type_means[$data['pay_type']] ?? '-';

    }



    public function getStatusMeansAttr( $value,$data)

    {

        $status_means=[

            1=>'待付款',

            2=>'已付款',

            -1=>'已取消',

        ];

        return $status_means[$data['status']] ?? '-';

    }

    public function partnerCommission(){

        return  $this->hasMany(PartnerCommissionModel::class,'order_id','order_id')->where(['type'=>'收银台']);

    }

}



